package com.hopu.controller;

import com.hopu.domain.ContractProductC;
import com.hopu.domain.Factoryc;
import com.hopu.service.ContractProductCService;
import com.hopu.service.ExtCproductCService;
import com.hopu.service.FactorycService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@RequestMapping("/Product")
@Controller
public class ContractProductController {
    @Autowired
    FactorycService factorycService;

    @Autowired
    ContractProductCService productCService;

    @Autowired
    ExtCproductCService extCproductCService;

    @RequestMapping("/contractproduct")
    public  String contractproduct(String contractId, Model model){
        model.addAttribute("contractId",contractId);
        String state="1";
        List<Factoryc> factorycs = factorycService.findState(state);
        model.addAttribute("factoryList",factorycs);

        List<ContractProductC> contractProductCS = productCService.selectByPrimaryKey(contractId);


        for (ContractProductC contractProductC : contractProductCS) {
            contractProductC.setAmount(contractProductC.getPrice()*contractProductC.getCnumber());
        }
        model.addAttribute("dataList",contractProductCS);
        return "/basicinfo/contract/jContractProductCreate";
    }

    @RequestMapping("/insert")
    public String insert(ContractProductC contractProductC,String contractId,Model model){

        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        contractProductC.setContractProductId(uuid);
        model.addAttribute("contractId",contractId);

        int insert = productCService.insert(contractProductC);
        return "redirect:contractproduct";
    }

    @RequestMapping("/deleteById")
    public String deleteById(String[] contractProductId,String contractId,Model model){
        //删除所有附件，这个货物的
        int i1 = extCproductCService.deleteByContractProductById(contractProductId);
        System.out.println("我是级联删除===>>"+i1);
        if(i1!=0){
            //删除货物
            if (contractProductId!=null){
                for (String s : contractProductId) {
                    int i = productCService.deleteByPrimaryKey(s);
                }
            }
        }

        model.addAttribute("contractId",contractId);
        return "redirect:contractproduct";
    }

    @RequestMapping("/toupdate")
    public String toupdate(String contractProductId,Model model){
        ContractProductC contractProductC = productCService.selectPrimaryKey(contractProductId);
        model.addAttribute("obj",contractProductC);

        String state="1";
        List<Factoryc> factorycs = factorycService.findState(state);
        model.addAttribute("factoryList",factorycs);

        return "/basicinfo/contract/jContractProductUpdate";
    }
    @RequestMapping("/update")
    public String update(ContractProductC contractProductC,Model model){
        System.out.println("contractProductC.getContractProductId() = " + contractProductC.getContractProductId());
        String factoryId = contractProductC.getFactoryId();
        model.addAttribute("contractId",contractProductC.getContractId());
        int i = productCService.updateByPrimaryKey(contractProductC);
        return "redirect:contractproduct";
    }

   @RequestMapping("/jump")
    public String jump(String contractProductId,Model model){
       System.out.println("contractProductId = " + contractProductId);
       ContractProductC contractProductC = productCService.selectPrimaryKey(contractProductId);
       String contractId = contractProductC.getContractId();
       model.addAttribute("contractId",contractId);
       return "redirect:contractproduct";
    }
}
